package com.weihuachao.repair;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.lidroid.xutils.http.RequestParams;
import com.weihuachao.entity.Result;
import com.weihuachao.https.BasicRequestCallBck;
import com.weihuachao.https.XUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Virgo on 2016/8/4.
 */
public class RepasswordActivity extends Activity {

    @BindView(R.id.oldpass_repassword)
    EditText oldpassRepassword;
    @BindView(R.id.newpass_repassword)
    EditText newpassRepassword;
    @BindView(R.id.renewpass_repassowrd)
    EditText renewpassRepassowrd;
    @BindView(R.id.back_repassword)
    Button backRepassword;
    @BindView(R.id.submit_repassword)
    Button submitRepassword;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_repassword);
        XUtils.init(this);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.oldpass_repassword, R.id.newpass_repassword, R.id.renewpass_repassowrd, R.id.back_repassword, R.id.submit_repassword})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back_repassword:
                finish();
                break;
            case R.id.submit_repassword:
                repassword();
                break;
        }
    }

    private void repassword() {
        if (newpassRepassword.getText().toString().equals(oldpassRepassword.getText().toString())) {
            XUtils.show("新旧密码一直，禁止修改！");
            return;
        }
        if (newpassRepassword.getText().toString().equals(renewpassRepassowrd.getText().toString()) && newpassRepassword.getText().toString().length() > 5) {
            String oldpass = oldpassRepassword.getText().toString();
            String newpass = newpassRepassword.getText().toString();
            RequestParams params = new RequestParams();
            params.addBodyParameter("user.phone", com.weihuachao.app.application.getAccount());
            params.addBodyParameter("user.oldpass", oldpass);
            params.addBodyParameter("user.pwd", newpass);
            XUtils.send(XUtils.UPDATE, params, new BasicRequestCallBck<Result<Boolean>>() {
                @Override
                public void success(Result<Boolean> data) {
                    if (data.state == 2) {
                        Toast.makeText(RepasswordActivity.this, data.description, Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(RepasswordActivity.this, data.description + "请重新登录！", Toast.LENGTH_SHORT).show();
                        com.weihuachao.app.application.setUid(0);
                        com.weihuachao.app.application.setAccount(null);
                        startActivity(new Intent(RepasswordActivity.this, LoginActivity.class));
                        finish();
                    }
                }
            });
        } else {
            XUtils.show("请确认两次输入的密码一样,并且密码长度最少为6为！");
        }
    }
}
